﻿#if MYSQL_6_9 || MYSQL_6_10

using Externals.MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

using static Apewer.Source.SourceUtility;

namespace Apewer.Source
{

    /// <summary>连接 MySQL 数据库的客户端。</summary>
    public class MySql : DbClient
    {

        #region connection

        private MySqlConnection _conn = null;
        private string _connstr = null;

        /// <summary>使用连接字符串创建数据库连接实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentException"></exception>
        public MySql(IDbConnection connection, Timeout timeout = null) : base(timeout)
        {
            if (connection == null) throw new ArgumentNullException(nameof(connection), "指定的连接无效。");

            var conn = connection as MySqlConnection;
            if (conn == null) throw new ArgumentException(nameof(connection), "指定的连接不是支持的类型。");

            _conn = conn;
            _connstr = conn.ConnectionString;
        }

        /// <summary>创建实例。</summary>
        public MySql(string connnectionString, Timeout timeout = default) : base(timeout)
        {
            _connstr = connnectionString;
        }

        /// <summary>构建连接字符串以创建实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        public MySql(string address, string store, string user, string pass, Timeout timeout = null) : base(timeout)
        {
            if (string.IsNullOrEmpty(address)) throw new ArgumentNullException(nameof(address));
            if (string.IsNullOrEmpty(store)) store = "mysql";
            if (string.IsNullOrEmpty(user)) user = "root";
            var cs = $"server={address}; database={store}; uid={user}; pwd={pass ?? ""}; ";
            _connstr = cs;
        }

        /// <summary>构建连接字符串以创建实例。</summary>
        /// <exception cref="ArgumentNullException"></exception>
        public MySql(string address, int port, string store, string user, string pass, Timeout timeout = null) : base(timeout)
        {
            if (string.IsNullOrEmpty(address)) throw new ArgumentNullException(nameof(address));
            if (port < 1 || port > 65535) throw new ArgumentOutOfRangeException(nameof(port));
            if (string.IsNullOrEmpty(store)) store = "mysql";
            if (string.IsNullOrEmpty(user)) user = "root";
            var cs = $"server={address}; port={port}; database={store}; uid={user}; pwd={pass ?? ""}; ";
            _connstr = cs;
        }

        #endregion

        #region override

        /// <summary></summary>
        public override string ConnectionString { get => _connstr; }

        /// <summary></summary>
        protected override IDataAdapter CreateDataAdapter(IDbCommand command) => new MySqlDataAdapter((MySqlCommand)command);

        /// <summary></summary>
        protected override IDbConnection GetConnection()
        {
            if (_conn == null) _conn = new MySqlConnection(_connstr);
            return _conn;
        }

        /// <summary></summary>
        protected override IDataParameter CreateParameter() => new MySqlParameter();

        /// <summary></summary>
        public override string[] StoreNames()
        {
            throw new NotImplementedException();
        }

        /// <summary></summary>
        public override string[] TableNames()
        {
            var store = StoreName();
            var sql = $"select table_name from information_schema.tables where table_schema='{store}' and table_type='base table'";
            return QueryStrings(sql);
        }

        /// <summary></summary>
        public override string[] ColumnNames(string tableName)
        {
            var store = StoreName();
            var table = TextUtility.AntiInject(tableName);
            var sql = $"select column_name from information_schema.columns where table_schema='{store}' and table_name='{table}'";
            return QueryStrings(sql);
        }

        /// <summary></summary>
        protected override string Initialize(TableStructure structure, string table)
        {
            if (string.IsNullOrEmpty(table)) table = structure.TableName;

            // 检查现存表。
            var exists = false;
            var tables = TableNames();
            if (tables.Length > 0)
            {
                var lower = table.ToLower();
                foreach (var tn in tables)
                {
                    if (TextUtility.IsEmpty(tn)) continue;
                    if (tn.ToLower() == lower)
                    {
                        exists = true;
                        break;
                    }
                }
            }

            if (exists)
            {
                var columns = ColumnNames(table);
                if (columns.Length > 0)
                {
                    var lower = new List<string>(columns.Length);
                    var added = 0;
                    foreach (var column in columns)
                    {
                        if (TextUtility.IsBlank(column)) continue;
                        lower.Add(column.ToLower());
                        added++;
                    }
                    lower.Capacity = added;
                    columns = lower.ToArray();
                }
                var sqlsb = new StringBuilder();
                foreach (var column in structure.Columns)
                {
                    // 检查 Independent 特性。
                    if (structure.Independent && column.Independent) continue;

                    // 去重。
                    var lower = column.Field.ToLower();
                    if (columns.Contains(lower)) continue;

                    var type = Declaration(column);
                    if (type.IsEmpty()) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");

                    // alter table `_record` add column `_index` bigint;
                    sqlsb.Append("alter table `", table, "` add column ", type, "; ");
                }
                var sql = sqlsb.ToString();
                if (sql.IsEmpty()) return null;

                var execute = Execute(sql, null, false);
                if (!execute.Success) return execute.Message;

                // 设置主键。
                foreach (var column in structure.Columns)
                {
                    if (column.PrimaryKey)
                    {
                        var sql_pk = $"alter table {structure.TableName} add primary key {column.Field}";
                        Execute(sql_pk, null, false);
                    }
                }

                return null;
            }
            else
            {
                // create table _record (`_index` bigint, `_key` varchar(255),  `_text` longtext) engine=innodb default charset=utf8mb4

                var columns = new List<string>(structure.Columns.Length);
                var columnsAdded = 0;
                var primarykeys = new List<string>();
                foreach (var column in structure.Columns)
                {
                    // 检查 Independent 特性。
                    if (structure.Independent && column.Independent) continue;

                    // 字段。
                    var type = Declaration(column);
                    if (type.IsEmpty()) return TextUtility.Merge("类型 ", column.Type.ToString(), " 不受支持。");
                    if (!column.Independent)
                    {
                        if (column.PrimaryKey) primarykeys.Add(column.Field);
                        if (column.Incremental) type += " auto_increment";
                    }

                    columns.Add(type);
                    columnsAdded++;
                }
                columns.Capacity = columnsAdded;
                var joined = string.Join(", ", columns);

                // 设置主键。
                string sql;
                var sqlPrimaryKey = primarykeys.Count < 1 ? "" : (", primary key (" + string.Join(", ", primarykeys.ToArray()) + ")");
                sql = TextUtility.Merge("create table `", table, "`(", joined, sqlPrimaryKey, ") engine=innodb default charset=utf8mb4; ");

                var execute = Execute(sql, null, false);
                return execute.Success ? null : execute.Message;
            }
        }

        /// <summary>插入记录。返回错误信息。</summary>
        public override string Insert(object record, string table = null, bool adjust = true)
        {
            if (record == null) return "参数无效。";
            if (adjust) SourceUtility.FixProperties(record);

            var structure = TableStructure.Parse(record.GetType());
            if (structure == null) return "无法解析记录模型。";
            if (string.IsNullOrEmpty(table)) table = structure.TableName;
            if (string.IsNullOrEmpty(table)) return "表名称无效。";

            // 排除字段。
            var excluded = new List<string>();
            foreach (var ca in structure.Columns)
            {
                // 排除不使用 ORM 的属性。
                if (ca.Independent || ca.Incremental) excluded.Add(ca.Field);
            }

            var ps = structure.CreateParameters(record, Parameter, excluded);
            var psc = ps.Length;
            if (psc < 1) return "数据模型不包含字段。";

            var names = new List<string>(psc);
            var values = new List<string>(psc);
            foreach (var p in ps)
            {
                var pn = p.ParameterName;
                names.Add("`" + p + "`");
                values.Add("@" + p);
            }
            var ns = string.Join(", ", names);
            var vs = string.Join(", ", values);
            var sql = $"insert into {table} ({ns}) values ({vs}); ";

            var execute = Execute(sql, ps, false);
            if (execute.Success) return TextUtility.Empty;
            return execute.Message;
        }

        /// <summary>更新记录，实体中的 Key 属性不被更新。返回错误信息。</summary>
        /// <remarks>无法更新带有 Independent 特性的模型（缺少 Key 属性）。</remarks>
        public override string Update(IRecord record, string table = null, bool adjust = true)
        {
            if (record == null) return "参数无效。";
            if (adjust)
            {
                FixProperties(record);
                SetUpdated(record);
            }

            var structure = TableStructure.Parse(record.GetType());
            if (structure == null) return "无法解析记录模型。";
            if (structure.Independent) return "无法更新带有 Independent 特性的模型。";
            if (string.IsNullOrEmpty(table)) table = structure.TableName;
            if (string.IsNullOrEmpty(table)) return "表名称无效。";

            // 排除字段。
            var excluded = new List<string>();
            if (structure.Key != null) excluded.Add(structure.Key.Field);
            foreach (var ca in structure.Columns)
            {
                // 排除不使用 ORM 的属性、自增属性和主键属性。
                if (ca.Independent || ca.Incremental || ca.PrimaryKey || ca.NoUpdate) excluded.Add(ca.Field);
            }

            var ps = structure.CreateParameters(record, Parameter, excluded);
            var psc = ps.Length;
            if (psc < 1) return "数据模型不包含字段。";

            var items = new List<string>(psc);
            foreach (var p in ps)
            {
                var pn = p.ParameterName;
                items.Add(TextUtility.Merge("`", pn, "` = @", pn));
            }
            var key = record.Key.SafeKey();
            var sql = $"update {table} set {string.Join(", ", items)} where `{structure.Key.Field}`='{key}'; ";

            var execute = Execute(sql, ps, false);
            if (execute.Success) return TextUtility.Empty;
            return execute.Message;
        }

        /// <summary></summary>
        protected override string Keys(string tableName, string keyField, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select `{keyField}` from `{tableName}`";
            else return $"select `{keyField}` from `{tableName}` where `{flagField}` = {flagValue}";
        }

        /// <summary></summary>
        protected override string Get(string tableName, string keyField, string keyValue, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select * from `{tableName}` where `{keyField}` = '{keyValue}' limit 1";
            else return $"select * from `{tableName}` where `{keyField}` = '{keyValue}' and `{flagField}` = {flagValue} limit 1";
        }

        /// <summary></summary>
        protected override string List(string tableName, string flagField, long flagValue)
        {
            if (flagValue == 0) return $"select * from `{tableName}`";
            else return $"select * from `{tableName}` where `{flagField}` = {flagValue}";
        }

        #endregion

        #region special

        string StoreName() => Internals.TextHelper.ParseConnectionString(_connstr).GetValue("database") ?? "";

        /// <summary>获取所有视图的名称。</summary>
        public string[] ViewNames()
        {
            var store = StoreName();
            var sql = $"select table_name from information_schema.tables where table_schema='{store}' and table_type='view'";
            return QueryStrings(sql);
        }

        /// <summary>获取记录。</summary>
        /// <param name="model">填充的记录模型。</param>
        /// <param name="skip">要跳过的记录数，可用最小值为 0。</param>
        /// <param name="count">要获取的记录数，可用最小值为 1。</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        /// <exception cref="ModelException"></exception>
        public T[] Range<T>(Type model, int skip, int count) where T : class, new()
        {
            if (model == null) throw new ArgumentNullException(nameof(model));
            if (skip < 0) throw new ArgumentOutOfRangeException(nameof(skip));
            if (count < 1) throw new ArgumentOutOfRangeException(nameof(count));

            var ts = TableStructure.Parse(model);
            if (ts.TableName.IsEmpty()) throw ModelException.InvalidTableName(model);

            var sql = $"select * from `{ts.TableName}` limit {skip}, {count}";
            using (var query = Query(sql))
            {
                if (!query.Success) throw new SqlException(query, sql);
                var array = query.Fill<T>();
                return array;
            }
        }

        /// <summary>对表添加列，返回错误信息。</summary>
        /// <typeparam name="T">记录类型。</typeparam>
        /// <param name="column">列名称。</param>
        /// <param name="type">字段类型。</param>
        /// <param name="length">字段长度，仅对 VarChar 和 NVarChar 类型有效。</param>
        /// <returns></returns>
        public string AddColumn<T>(string column, ColumnType type, int length = 0) where T : class, IRecord
        {
            var columnName = column.SafeName();
            if (columnName.IsEmpty()) return "列名无效。";

            var ta = TableAttribute.Parse(typeof(T));
            if (ta == null) return "无法解析记录模型。";
            var tableName = ta.Name;

            var columeType = "";
            switch (type)
            {
                case ColumnType.Integer:
                    columeType = "bigint";
                    break;
                case ColumnType.Float:
                    columeType = "double";
                    break;
                case ColumnType.Bytes:
                    columeType = "longblob";
                    break;
                case ColumnType.DateTime:
                    columeType = "datetime";
                    break;
                case ColumnType.VarChar:
                case ColumnType.NVarChar:
                    columeType = $"varchar({length})";
                    break;
                case ColumnType.VarChar191:
                case ColumnType.NVarChar191:
                    columeType = "varchar(191)";
                    break;
                case ColumnType.VarCharMax:
                case ColumnType.NVarCharMax:
                    columeType = "varchar(max)";
                    break;
                case ColumnType.Text:
                    columeType = "longtext";
                    break;
            }
            if (columeType.IsEmpty()) return "类型不支持。";

            var sql = $"alter table `{tableName}` add {columnName} {columeType}; ";
            var execute = Execute(sql, null, false) as Execute;
            var error = execute.Message;
            return error;
        }

        #endregion

        #region static

        /// <summary>清空与指定连接关联的连接池。</summary>
        public static void ClearPool(IDbConnection connection)
        {
            var special = connection as MySqlConnection;
            if (special != null)
            {
                try
                {
                    MySqlConnection.ClearPool(special);
                }
                catch { }
            }
        }

        /// <summary>清空与指定连接关联的连接池。</summary>
        public static void ClearPool(IDbAdo instance)
        {
            var connection = instance?.Connection;
            if (connection != null) ClearPool(connection);
        }

        /// <summary>清空连接池。</summary>
        public static void ClearAllPools()
        {
            try
            {
                MySqlConnection.ClearAllPools();
            }
            catch { }
        }

        /// <summary></summary>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        internal static MySqlParameter Parameter(Parameter parameter)
        {
            if (parameter == null) throw new InvalidOperationException("参数无效。");
            return Parameter(parameter.Name, parameter.Type, parameter.Size, parameter.Value);
        }

        /// <summary></summary>
        internal static MySqlParameter Parameter(string name, ColumnType type, Int32 size, object value)
        {
            if (TextUtility.IsBlank(name)) return null;

            var dbtype = MySqlDbType.Int64;
            switch (type)
            {
                case ColumnType.Bytes:
                    dbtype = MySqlDbType.LongBlob;
                    break;
                case ColumnType.Integer:
                    dbtype = MySqlDbType.Int64;
                    break;
                case ColumnType.Float:
                    dbtype = MySqlDbType.Double;
                    break;
                case ColumnType.DateTime:
                    dbtype = MySqlDbType.DateTime;
                    break;
                case ColumnType.VarChar:
                case ColumnType.VarChar191:
                case ColumnType.VarCharMax:
                case ColumnType.NVarChar:
                case ColumnType.NVarChar191:
                case ColumnType.NVarCharMax:
                    dbtype = MySqlDbType.VarChar;
                    break;
                case ColumnType.Text:
                case ColumnType.NText:
                    dbtype = MySqlDbType.LongText;
                    break;
                default:
                    throw new InvalidOperationException(TextUtility.Merge("类型 ", type.ToString(), " 不受支持。"));
            }

            switch (type)
            {
                case ColumnType.VarChar:
                case ColumnType.NVarChar:
                    size = NumberUtility.Restrict(size, 0, 65535);
                    break;
                case ColumnType.VarChar191:
                case ColumnType.NVarChar191:
                    size = NumberUtility.Restrict(size, 0, 191);
                    break;
                default:
                    size = 0;
                    break;
            }

            if (value is string && value != null && size > 0)
            {
                value = TextUtility.Left((string)value, size);
            }

            var parameter = new MySqlParameter();
            parameter.ParameterName = name;
            parameter.MySqlDbType = dbtype;
            parameter.Value = value ?? DBNull.Value;
            if (size > 0) parameter.Size = size;
            return parameter;
        }

        /// <summary></summary>
        internal static MySqlParameter Parameter(string name, MySqlDbType type, object value, Int32 size = 0)
        {
            var parameter = new MySqlParameter();
            parameter.ParameterName = name;
            parameter.MySqlDbType = type;
            parameter.Value = value ?? DBNull.Value;
            if (size > 0) parameter.Size = size;
            return parameter;
        }

        private static string Declaration(ColumnAttribute column)
        {
            var type = TextUtility.Empty;
            var length = Math.Max(0, (int)column.Length);
            switch (column.Type)
            {
                case ColumnType.Integer:
                    type = "bigint";
                    break;
                case ColumnType.Float:
                    type = "double";
                    break;
                case ColumnType.Bytes:
                    type = "longblob";
                    break;
                case ColumnType.DateTime:
                    type = "datetime";
                    break;
                case ColumnType.VarChar:
                    type = TextUtility.Merge("varchar(", Math.Max(65535, length).ToString(), ")");
                    break;
                case ColumnType.VarChar191:
                    type = TextUtility.Merge("varchar(191)");
                    break;
                case ColumnType.VarCharMax:
                    type = TextUtility.Merge("varchar(max)");
                    break;
                case ColumnType.Text:
                    type = TextUtility.Merge("longtext");
                    break;
                case ColumnType.NVarChar:
                    type = TextUtility.Merge("varchar(", Math.Min(65535, length).ToString(), ")");
                    break;
                case ColumnType.NVarChar191:
                    type = TextUtility.Merge("varchar(191)");
                    break;
                case ColumnType.NVarCharMax:
                    type = TextUtility.Merge("varchar(65535)");
                    break;
                case ColumnType.NText:
                    type = TextUtility.Merge("longtext");
                    break;
                default:
                    return TextUtility.Empty;
            }
            return TextUtility.Merge("`", (string)column.Field, "` ", type);
        }

        /// <summary>获取每个数据库中，每个表的容量，单位为字节。</summary>
        public static Dictionary<string, Dictionary<string, long>> GetTablesCapacity(MySql source)
        {
            var result = new Dictionary<string, Dictionary<string, long>>();
            if (source != null && source.Connect().IsEmpty())
            {
                var sql = "select `table_schema`, `table_name`, `engine`, `data_length`, `index_length` from `information_schema`.tables order by `table_schema`, `table_name`";
                using (var query = (Query)source.Query(sql))
                {
                    for (var r = 0; r < query.Rows; r++)
                    {
                        var store = query.Text(r, "table_schema");
                        var table = query.Text(r, "table_name");
                        var engine = query.Text(r, "engine");
                        var capacity = NumberUtility.Int64(query.Text(r, "data_length")) + NumberUtility.Int64(query.Text(r, "index_length"));

                        if (store == "mysql") continue;
                        if (store == "information_schema") continue;
                        if (store == "performance_schema") continue;

                        if (engine != "MyISAM" && engine != "InnoDB") continue;

                        if (!result.ContainsKey(store)) result.Add(store, new Dictionary<string, long>());
                        if (!result[store].ContainsKey(table)) result[store].Add(table, 0L);
                        result[store][table] = capacity;
                    }
                }
            }
            return result;
        }

        #endregion

    }

}

#endif

